package templateFunc

import (
	. "controllers"
	"golanger.com/utils"
	// "jk/jklog"
	"strconv"
	"strings"
	"time"
)

func init() {
	App.AddTemplateFunc("UnixToStr", func(tm int64) string {
		if tm <= 0 {
			return ""
		}
		return utils.NewTime().UnixToStr(tm)
	})
	App.AddTemplateFunc("IncreaseOneSelf", func(index int) int {
		return index + 1
	})
	App.AddTemplateFunc("TimeDisplay", func(tm int64) string {
		if tm <= 0 {
			return ""
		}
		nowt := time.Now()
		now := nowt.Unix()
		inter := now - tm

		old := strings.Split(utils.NewTime().UnixToStr(tm), " ")

		result := ""

		if inter/60 == 0 { // find is minutes
			result = strconv.Itoa(int(inter)) + " 秒前"
		} else if (inter/60)/60 == 0 {
			result = strconv.Itoa(int(inter/60)) + " 分钟前"
		} else if (inter/3600)/24 == 0 {
			result = strconv.Itoa(int(inter/3600)) + " 小时 " + strconv.Itoa(int(inter%3600/60)) + "分钟前"
		} else {
			days := inter / 3600 / 24
			if days == 1 {
				result = "昨天 " + old[1]
			} else if days == 2 {
				result = "前天 " + old[1]
			} else {
				result = strconv.Itoa(int(days)+1) + " 天前"
			}
		}

		dateold := strings.Split(old[0], "-")
		year, _ := strconv.Atoi(dateold[0])
		month, _ := strconv.Atoi(dateold[1])
		// day, _ := strconv.Atoi(dateold[2])
		if nowt.Year()-year > 0 {
			result = strconv.Itoa(nowt.Year()-year) + " 年前"
		} else if int(nowt.Month())-month > 1 {
			result = strconv.Itoa(int(nowt.Month())-month) + " 个月前"
		}

		return result
	})
}
